import math
import time
import string

#Import wrapper libraries
import Skype4Py #Netbook -> Internet layer
import serial   #Netbook -> Circuits layer

#Custom Functions
#-----------------------------------------------
#Handshake function
#Attempts to complete a handshake with a device
#Returns the device signature if success, returns -1 if it fails.
def handshake(ser, max_tries, timeout):
	max_tries = math.fabs(max_tries) 			#Makes sure that the attempts value is positive
	tries = 0
	command = "FAILLINK"						#Defaults to a communication failure
	print "Sending info"
	ser.write("INIT0000") 						#Send the INIT command to the arduino
	while tries < max_tries:
		print "Attempt #" + str(tries) + "..."
		if ser.inWaiting() >= 8: 				#Wait until there are 8 bytes (At least 1 cmd)
			print "Command received"
			command = ser.read(8)   			#Read the cmd (8 bytes)
			tries = max_tries					#We are now done, exit
		else:
			tries = tries + 1
		time.sleep(timeout)						#timeout for a certain time
	return command 				 				#Return the results back

#Move function
#Attempts to move the vehicle. left and right denote the left and right motors
#and has a range of 0 - 255. 0 - 127 is backwards while 128 - 255 is forwards.
#Returns 0 if arduino confirms, returns -1 if it fails
def move_car(left, right):
	#we need to convert left and right into binary values
	return 0

#LED control
#Attempts to turn on an LED. value denotes the LED that is toggled.
#Returns 0 if arduino confirms, returns -1 if it fails
def LED_ctrl(value):
	return 0

#Request ID
#Requests for the UID of the arduino. This is used to figure out which USB port has
#which arduino. Returns 0 if arduino confirms, returns -1 if it fails
def req_ID():
	return 0